<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px"
             style="padding-left: 20px;"
    >
      <el-form-item label="创建人" prop="content">
        <el-input style="width: 140px" v-model="queryParams.createBy" placeholder="请输入人名" clearable size="small"
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="校区" prop="content">
        <el-select style="width: 140px" v-model="queryParams.schoolArea" @change="handleQuery"
                   placeholder="请选择校区" filterable clearable>
          <el-option v-for="item in studentList" :key="item.studentId" :label="item.studentName"
                     :value="item.studentName">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="日期" prop="date">
        <el-date-picker
          style="width: 140px"
          @change="handleQuery"
          clearable size="small"
          v-model="queryParams.workDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择工作日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="内容" prop="content">
        <el-input style="width: 140px" v-model="queryParams.content" placeholder="请输入内容" clearable size="small"
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="messageList" :row-style="{height:'40px'}" :cell-style="{padding:'0px'}">
      <el-table-column v-if="false" type="selection" width="55" align="center" />
      <el-table-column label="主要内容" align="left" prop="content" >
        <template slot-scope="scope">
          <div> {{ getContent(scope.row.content) }} </div>
        </template>
      </el-table-column>
      <el-table-column label="校区" width="90" align="center" prop="schoolArea" />
      <el-table-column label="创建人" width="90" align="center" prop="createBy" />
      <el-table-column label="工作日期"  width="120" align="center" prop="workDate" />
      <el-table-column v-if="false" label="创建时间"  width="180" align="center" prop="createTime" />
      <el-table-column label="操作"  width="160" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleView(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            v-if="Date.now() < Date.parse(scope.row.workDate + ' 23:59:59')"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['daily:work:delete']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"    />

    <!-- 添加或修改评论管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled="formDisabled">
        <el-form-item label="内容" prop="content">
          <el-input style="margin: 5px 0" autosize v-model="form.content1" type="textarea" show-word-limit maxlength="200"></el-input>
          <el-input style="margin: 5px 0" autosize v-model="form.content2" type="textarea" show-word-limit maxlength="700"></el-input>
          <el-input style="margin: 5px 0" autosize v-model="form.content3" type="textarea" show-word-limit maxlength="700"></el-input>
          <el-input style="margin: 5px 0" autosize v-model="form.content4" type="textarea" show-word-limit maxlength="200"></el-input>
          <el-input style="margin: 5px 0" autosize v-model="form.content5" type="textarea" show-word-limit maxlength="200"></el-input>
          <el-input style="margin: 5px 0" autosize v-model="form.content6" type="textarea" show-word-limit maxlength="200"></el-input>
          <el-input style="margin: 5px 0" autosize v-model="form.content7" type="textarea" show-word-limit maxlength="400"></el-input>
          <el-input style="margin: 5px 0" autosize v-model="form.content8" type="textarea" show-word-limit maxlength="400"></el-input>
          <el-input style="margin: 5px 0" autosize v-model="form.content9" type="textarea" show-word-limit maxlength="200"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  mapGetters
} from 'vuex'
import Emoji from '@/components/Emoji'
import {delDailyWork, getDailyWork, listDailyWork, updateDailyWork} from "@/api/teach/dailyWork";
import Tinymce from "@/components/Tinymce/index.vue";
import {listStudentAll} from "@/api/teach/student";

export default {
  name: 'Message',
  components: {
    Tinymce,
    Emoji
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      names: [],
      // 非单个禁用
      single: true,
      formDisabled: false,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 留言管理表格数据
      messageList: [],
      //校区
      studentList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: null,
        likeNum: null,
        content: null,
        type: null,
        blogId: null,
        userId: null,
        delFlag: null,
        createBy: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      toName: '',
      isAdmin: false,
      cursorIndexStart: null,//光标选中开始的位置
      cursorIndexEnd: null//光标选中结束的位置
    }
  },
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  created() {
    this.getList()
    this.getStudentsList()
  },
  mounted: function() {
    this.$nextTick(function() {
      // 仅在整个视图都被渲染之后才会运行的代码
    })
  },
  methods: {
    /** 查询留言管理列表 */
    getList() {
      listDailyWork(this.queryParams).then(response => {
        // console.log(response)
        this.loading = false
        this.messageList = response.rows
        this.total = response.total
        this.loading = false;
      })
    },
    /** 查询校区信息列表 */
    getStudentsList() {
      listStudentAll(this.queryStudentParams).then(response => {
        this.studentList = response.rows;
        // console.log(this.studentList);
      });
    },
    getContent(content) {
      if (content === ''){
        return "未能正确识别工作内容"
      }
      let message = content.replace(/<\/?[^>]+(>|$)/g, "");
      // console.log(message);
      let split = message.split(/[\s\n]+/);
      if (split[0].length > 1){
        return split[0] + ' ' + split[1] + ' ' + split[2]
      }
      else {
        return split[1] + ' ' + split[2] +  ' ' + split[3]
      }
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        mainId: null,
        likeNum: null,
        content: null,
        type: null,
        blogId: null,
        userId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      // console.log(this.queryParams)
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.createBy = ''
      this.queryParams.schoolArea = null
      this.queryParams.workDate = null
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(mes) {
      this.reset()
      if (mes.mainId != null) {
        this.form.mainId = mes.mainId
      } else {
        this.form.mainId = mes.id
      }
      this.form.parentId = mes.id
      this.form.type = '1'
      this.form.createBy = this.$store.getters.name
      this.toName = '@' + mes.createBy
      this.open = true
      this.title = '回复留言'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      this.formDisabled = false
      getDailyWork(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改日报'
      })
    },
    /** 查看按钮操作 */
    handleView(row) {
      const id = row.id || this.ids
      getDailyWork(id).then(response => {
        this.form = response.data
        // console.log(response.data)
        this.formDisabled = true
        this.open = true
        this.title = '日报浏览'
      })
    },
    getContentBlob(form){
      let text = '课程具体情况:' + this.isContentUndefined(form.content1) + '\n'
      text = text + '迟到信息统计:' + this.isContentUndefined(form.content2) + '\n'
      text = text + '客户到访统计:' + this.isContentUndefined(form.content3) + '\n'
      text = text + '教学事故汇报:' + this.isContentUndefined(form.content4) + '\n'
      text = text + '客户反馈建议:' + this.isContentUndefined(form.content5) + '\n'
      text = text + '学生测试统计:' + this.isContentUndefined(form.content6) + '\n'
      text = text + '单词考察统计:' + this.isContentUndefined(form.content7) + '\n'
      text = text + '热线记录统计:' + this.isContentUndefined(form.content8) + '\n'
      return  text + '其他事宜记录:' + form.content9
    },
    isContentUndefined(val) {
      if (val === undefined || val === '') {
        return '无'
      }
      return val
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.content = this.getContentBlob(this.form)
          // console.log(this.form)
          updateDailyWork(this.form).then(response => {
            this.$modal.msgSuccess('修改成功')
            this.open = false
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      // console.log(row)
      this.$modal.confirm('是否确认删除时间为"' + row.createTime + '"的内容？').then(function() {
        return delDailyWork(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
        this.$modal.msg("取消删除")
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cms/message/export', {
        ...this.queryParams
      }, `message_${new Date().getTime()}.xlsx`)
    },
    blur(e) {
      this.cursorIndexStart = e.srcElement.selectionStart  // 获取input输入框失去焦点时光标选中开始的位置
      this.cursorIndexEnd = e.srcElement.selectionEnd  // 获取input输入框失去焦点时光标选中结束的位置
    },
    output(val) {
      if (this.cursorIndexStart !== null && this.form.content) {
        //如果 文本域获取了焦点, 则在光标位置处插入对应字段内容
        this.form.content = this.form.content.substring(0, this.cursorIndexStart) + val + this.form.content.substring(this.cursorIndexEnd)
      } else {
        // 如果 文本域未获取焦点, 则在字符串末尾处插入对应字段内容
        this.form.content = this.form.content ? this.form.content : '' + val
      }
    }
  }
}
</script>

<style scoped>
.comment {
  border-bottom: 1px dashed #ccc;
  margin: 10px 0;
  display: flex;
}

.el-avatar {
  width: 35px;
  height: 35px;
  /* border: 2px solid white; */
  box-shadow: 0 0 10px 2px rgba(0, 0, 0, .06);
  flex-shrink: 0;
}

.content {
  text-align: left;
  font-size: 14px;
  flex-grow: 1;
}

.nkname {
  margin-left: 10px;
  max-width: 530px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.rp,
.date {
  color: #999;
  margin-left: 10px;
}

.pcreateBy {
  margin-left: 10px;
}

.blog {
  color: #349edf;
  margin-left: 10px;
  cursor: pointer;
}

.reply {
  margin-left: 10px;
}

.op {
  color: #ddd;
  font-weight: lighter;
}

.rep {
  color: #349edf;
}

.del {
  color: red;
}

.op:hover {
  cursor: pointer;
}

.el-table__empty-text {
  line-height: 60px;
  width: 50%;
  color: #909399;
}
</style>
